package com.zzt.zt_cropimageview;

import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zzt.zt_cropimageview.act.ActImgV1;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private ImagePicker imagePicker;
    //请求状态码
    private static int PERMISSION_REQUEST_CODE = 1111;
    public static final int REQUEST_CODE_CROP = 1002;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imagePicker = ImagePicker.getInstance();
        takePicture();
        imageView = findViewById(R.id.imageView);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!checkPermission()) {
                    requestPermission();
                } else {
                    GLImage glImage = GLImage.Builder.newBuilder()
                            .setAddTime(TimeUtil.getNow_millisecond())
                            .setPath(imagePicker.getTakeImageFile().getAbsolutePath()).setMimeType("image/jpeg").build();
                    imagePicker.clearSelectedImages();
                    imagePicker.addSelectedImageItem(glImage, true);
                    ImageCropActivity.start(MainActivity.this, REQUEST_CODE_CROP);
                }
            }
        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActImgV1.start(v.getContext());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_CROP) {
            String path = data.getStringExtra(Extras.EXTRA_FILE_PATH);
            if (path != null) {
                Log.e(TAG, "path=" + path);
                //本地文件
                File file = new File(path);
                //加载图片
                Glide.with(this).load(file).into(imageView);
            }
        }
    }

    private void takePicture() {
        File takeImageFile = null;
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePictureIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if (this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
            if (Utils.existSDCard()) {
                takeImageFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/camera/");
            } else {
                takeImageFile = Environment.getDataDirectory();
            }

            takeImageFile = new File(takeImageFile, "IMG_12345.jpg");
//            takeImageFile = new File(takeImageFile, "1602308626520.jpg");
        }

        if (takeImageFile == null) {
            return;
        }
        imagePicker.setTakeImageFile(takeImageFile);
    }


    /**
     * 建仓是否有文件存储权限
     *
     * @return
     */
    private boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE);
        int result1 = ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED
                && result1 == PackageManager.PERMISSION_GRANTED
                ;
    }

    /**
     * 请求权限
     */
    private void requestPermission() {
        //below android 11
        ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE,
                READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
    }
}